<?php
// +----------------------------------------------------------------------
// | Author: 黄柯柯 <keke.huang@ostengar.com>
// +----------------------------------------------------------------------
namespace app\user\service;

use app\user\model\UserFollowModel;
use app\user\model\UserModel;
use think\Db;
use think\db\Query;

class UserFollowService
{

    /**
     * 关注用户列表
     * @param      $filter
     * @param int  $filter['per_page'] 每页数据 默认10
     * @return     $result
     * @throws \think\exception\DbException
     */
    public function followUserList($filter)
    {
        $filter['user_id'] = cmf_get_current_user_id();

        $status = empty($filter['status']) ? 2 : $filter['status'];
        if ($status == 1) {
            $join = [
                ['__USER__ u', 'a.follow_user_id = u.id'],
            ];
        } else {
            $join = [
                ['__USER__ u', 'a.user_id = u.id'],
            ];
        }
        $field    = 'u.*,u.id as uId,a.*';
        $per_page = empty($filter['per_page']) ? 10 : $filter['per_page'];

        $userFollowModel = new UserFollowModel();
        $result          = $userFollowModel->alias('a')->join($join)->field($field)->where(function (Query $query) use ($filter, $status) {

            if ($status == 1) { //粉丝
                $query->where('a.follow_user_id ', $filter['user_id']);
            } else { //关注
                $query->where('a.user_id', $filter['user_id']);
            }

        })->paginate($per_page);
        return $result;
    }

    /**
     * 关注用户
     * @param      $filter
     * @return     $result
     * @throws \think\exception\DbException
     */
    public function followUser($filter)
    {

        // 开启事务
        Db::startTrans();
        try {
            $userModel       = new UserModel();
            $userFollowModel = new UserFollowModel();
            //当前用户的关注数加一
            $userModel->where('id', $filter['user_id'])->setInc('follow_count');
            //被关注用户的粉丝数加一
            $userModel->where('id', $filter['follow_user_id'])->setInc('fans_count');
            // 判断他是否关注了我
            $where             = ['user_id' => $filter['follow_user_id'], 'user_id' => $filter['follow_user_id']];
            $checkUserFollowMe = $userFollowModel->where($where)->find();
            if (!empty($checkUserFollowMe)) { //我们相互关注了
                $filter['status'] = 1;
                $userFollowModel->where($where)->update(['status' => 1]);
            }
            //将关注记录写入关注表中
            $userFollowModel->allowField(true)->data($filter, true)->isUpdate(false)->save();
            // 关闭事务
            Db::commit();
        } catch (\Exception $e) {
            // 事务回滚
            Db::rollback();
            return 0;
        }
        return true;
    }

    /**
     * 取消关注用户
     * @param      $filter
     * @return     $result
     * @throws \think\exception\DbException
     */
    public function cancelFollowUser($filter)
    {

        // 开启事务
        Db::startTrans();
        try {
            $userModel       = new UserModel();
            $userFollowModel = new UserFollowModel();
            //当前用户的关注数减一
            $userModel->where('id', $filter['user_id'])->setDec('follow_count');
            //被关注用户的粉丝数加减一
            $userModel->where('id', $filter['follow_user_id'])->setDec('fans_count');
            //将关注记删除
            $userFollowModel->where($filter)->delete();
            // 判断他是否关注了我
            $where             = ['user_id' => $filter['follow_user_id'], 'user_id' => $filter['follow_user_id']];
            $checkUserFollowMe = $userFollowModel->where($where)->find();
            if (!empty($checkUserFollowMe)) { //我们相互关注了
                $userFollowModel->where($where)->update(['status' => 0]);
            }
            //删除用户操作日志
            $follow_user_id = $filter["follow_user_id"];
            Db::name('user_action_log')->where(['user_id' => $filter['user_id'], 'object' => "userFollow$follow_user_id"])->delete();
            // 关闭事务
            Db::commit();
        } catch (\Exception $e) {
            // 事务回滚
            Db::rollback();
            return 0;
        }

        return true;
    }

}
